package com.atguigu.tingshu.account.receive;


import cn.hutool.core.collection.CollectionUtil;

import com.atguigu.tingshu.account.service.UserAccountService;

import com.atguigu.tingshu.common.rabbit.constant.MqConst;

import com.rabbitmq.client.Channel;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


import java.util.Map;

@Component
@Slf4j
public class AccountReceive {
    @Autowired
    private UserAccountService userAccountService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.ROUTING_USER_REGISTER, durable = "true"),
            exchange = @Exchange(value = MqConst.EXCHANGE_USER, durable = "true"),
            key = MqConst.ROUTING_USER_REGISTER
    ))
    @SneakyThrows
    public void register(Map map, Message message, Channel channel) {
        if (CollectionUtil.isNotEmpty(map)) {
            log.info("[账户服务]监听到用户首次注册初始化账户消息：{}", map);
            userAccountService.saveUserAccount(map);
        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);

    }


}
